
var prevMainmenuID;
var logined;
var loginuser;

var Controller = {

	//-------------------------------------------------------------------------
	// Set selected main menu item new CSS style, and restore the previous one.
	resetMainmenuCSS : function(selectedid) {
		
		$(selectedid).removeClass("bad_mi_pagination");
		$(selectedid).addClass("bad_mi_pagination_selected");
		if ( prevMainmenuID != selectedid ) {
			$(prevMainmenuID).removeClass("bad_mi_pagination_selected");
			$(prevMainmenuID).addClass("bad_mi_pagination");
		}
		
		prevMainmenuID = selectedid;
	},	
	
	loadRecommendedProductsINDBRT : function() {
		$.ajax(
				{
					url:"/bademo/rtscore/indb",
					async: true,
					dataType: "html",
					success: function(data) {
						$("#List_Realtime_Recommend").empty().html(data);
					},
					error: function(data) {
						Controller.alertInformation(
								"Requesting recommended products failed.", 600, 170);
					}
				}
			);		
	},

	loadRecommendedProductsEXDBRT : function() {
		$.ajax(
				{
					url:"/bademo/rtscore/exdb",
					async: true,
					dataType: "html",
					success: function(data) {
						$("#List_Realtime_Recommend").empty().html(data);
					},
					error: function(data) {
						Controller.alertInformation(
								"Requesting recommended products failed.", 600, 170);
					}
				}
			);		
	},	

	batchRunEXDB : function() {
		$.ajax(
				{
					url:"/bademo/batch/exdb",
					async: true,
					dataType: "html",
					success: function(data) {
						Controller.alertInformation(
								"Ex-database batch job is done.", 700, 170);
					},
					error: function(data) {
						Controller.alertInformation(
								"Requesting ex-database batch job failed.", 700, 170);
					}
				}
			);		
	},	
	
	batchRunINDB : function() {
		$.ajax(
				{
					url:"/bademo/batch/indb",
					async: true,
					dataType: "html",
					success: function(data) {
						Controller.alertInformation(
								"In-database batch job is done.", 700, 170);
					},
					error: function(data) {
						Controller.alertInformation(
								"Requesting in-database batch job failed.", 700, 170);
					}
				}
			);		
	},	
	
	batchShowScoreEXDB : function() {

		$.ajax({
			url: "/bademo/stscore/exdb",
			async: "false",
			dataType: "html",
			type: "POST",
			data: {
				custId		:$("#Batch_Customer_ID").val()
			},
			success: function(data) {
				$("#List_Batch_Recommend").empty().html(data);
			},
			error: function(value) {
				Controller.alertInformation("FAILED to show score.", 600, 170);
			}
		});
		
	},
	
	batchShowScoreINDB : function() {

		$.ajax({
			url: "/bademo/stscore/indb",
			async: "false",
			dataType: "html",
			type: "POST",
			data: {
				custId		:$("#Batch_Customer_ID").val()
			},
			success: function(data) {
				$("#List_Batch_Recommend").empty().html(data);
			},
			error: function(value) {
				Controller.alertInformation("FAILED to show score.", 600, 170);
			}
		});		
		
	},
	
/*
	asyncRefreshTable : function(requestURL, isAsync, areaID, dom) {
		$.ajax(
			{
				url:requestURL,
				async: isAsync,	
				dataType: "html",
				success: function(data) {
					$(areaID).empty().html(data);
					dom.empty().html(data);
				},
				error: function() {
					Controller.alertInformation(
						"Request" + requestURL + " failed.", 600, 170);
				}
			}
		);
	},
*/
		
	//--------------------------------------------------------------------------
	// Connect handler function to button click events.
	//--------------------------------------------------------------------------
	connectEvent : function(){

		//----------------------------------------------------------------------
		// Bind click event of all tab page "Welcome".
		//----------------------------------------------------------------------
		$("#Tab_page_welcome").bind('click', 
			function() {
				$("#Main_content_area_to_show").html($("#Page_content_welcome").html());
				Controller.resetMainmenuCSS("#Tab_page_welcome");
			});
		
		//----------------------------------------------------------------------
		// Bind click event of all tab page "Real-time Scoring".
		//----------------------------------------------------------------------
		$("#Tab_page_Realtime").bind('click', 
				function() {
			
					if ( logined == 1 ) {
						$("#Main_content_area_to_show").html($("#Page_content_realtime_post").html());
					}
					else {
						$("#Main_content_area_to_show").html($("#Page_content_realtime_scoring").html());	
					}
					
					Controller.resetMainmenuCSS("#Tab_page_Realtime");
					
					// Bind "login" button event
					$("#Btn_Realtime_Login").bind('click',
						function() {
						
						// Get login id text
						if ( $("#Login_ID").val() == "" ) {
							Controller.alertInformation("Please specify Customer Name.", 700, 170);
							return;
						}
						// Get password text
						if ( $("#Login_Pass").val() == "" ) {
							Controller.alertInformation("Please specify Password.", 700, 170);
							return;
						}
						
						// post login request
						$.ajax({
							url: "/bademo/login/",
							async: "false",
							dataType: "html",
							type: "POST",
							data: {
								custId		:$("#Login_ID").val(),
								password	:$("#Login_Pass").val()
							},
							success: function(value) {
								logined = 1;
								loginuser = $("#Login_ID").val();
								$("#Label_Realtime_Customername").html(loginuser);
								
								$("#Main_content_area_to_show").html($("#Page_content_realtime_post").html());
								
								$("#Btn_Realtime_Recommend").bind('click', function () {
									
									// Check if the ex-database scoring is checked. Is true, then calls
									// ex-db scoring function, otherwise call in-db scoring function.
									if ( $("#Check_Realtime_Type").attr("checked") )
										Controller.loadRecommendedProductsEXDBRT();
									else
										Controller.loadRecommendedProductsINDBRT();
									
								});
							},
							error: function(value) {
								Controller.alertInformation("FAILED to Login.", 600, 170);
							}
						});
						
					});
					
				});
		
		//----------------------------------------------------------------------
		// Bind click event of "Batch Scoring".
		//----------------------------------------------------------------------
		$("#Tab_page_Batch").bind('click', 
				function() {
					$("#Main_content_area_to_show").html($("#Page_content_batch_scoring").html());
					Controller.resetMainmenuCSS("#Tab_page_Batch");
					
					// Bind "Run batch" event
					$("#Btn_Batch_Trigger").bind('click', function() {
						
						if ( $("#Check_Batch_Type").attr("checked") )
							Controller.batchRunEXDB();
						else
							Controller.batchRunINDB();
						
					});
					
					// Bind "Show Score" event
					$("#Btn_Batch_Show").bind('click', function() {
						
						// Check if the customer name is input
						if ( $("#Batch_Customer_ID").val() == "" )
							Controller.alertInformation("Please specify customer name firstly", 700, 170);
						else {
						
							if ( $("#Check_Batch_Type").attr("checked") )
								Controller.batchShowScoreEXDB();
							else
								Controller.batchShowScoreINDB();
							
						}
						
					});
					
				});		
		
	},
	
	alertInformation : function(Content, DiagWidth, DiagHeight) {
		
		// Set alert dialog content.
		$("#Modaldiag_alert").html(Content);
		
		// Show modal dialog.
		$("#Modaldiag_alert").dialog({
			modal		:true, 
			resizable	:false, 
			width		:DiagWidth, 
			height		:DiagHeight,
			buttons:{
				// OK button to confirm the capture.
				"OK":function() {
					$(this).dialog("close");
				}
			}
		}); // End of $("#Modaldiag_alert").dialog()
	},
	
	switchPage : function () {
		$("#Main_content_area_to_show").html($("#Page_content_welcome").html());
		Controller.resetMainmenuCSS("#Tab_page_welcome");
	},

	//-------------------------------------------------------------------------
	// Initialization functions.
	//-------------------------------------------------------------------------
	onLoad : function(){
		
		// initialize global variables
		logined = 0;
		
		// connect click events to javascript control functions
		this.connectEvent();
		// switch to the default tab page.		
		this.switchPage();
		
		
		
	}
};

//-----------------------------------------------------------------------------
// The main entry of IaaS SCE demo page.
//-----------------------------------------------------------------------------

$(function(){
	Controller.onLoad();
});
